package md

import (
	"bufio"
	"bytes"
	"dog-blog/app/post/models"
	"strings"
)

// ReadExtract 读取摘要
func ReadExtract(mdByte []byte) models.Article {
	article := models.Article{}

	input := bufio.NewScanner(bytes.NewReader(mdByte))
	for input.Scan() {
		bye := input.Bytes()

		if isSkip(bye) {
			continue
		}

		exist, value := readHideField(bye, "发布状态")
		if exist {
			if value == "否" {
				goto over
			}
			continue
		}

		exist, value = readHideField(bye, "文章ID")
		if exist {
			article.ID = value
			continue
		}

		exist, value = readNormalField(bye, "标题")
		if exist {
			article.Title = value
			continue
		}

		exist, value = readNormalField(bye, "分类")
		if exist {
			article.Category = value
			continue
		}

		existt, values := readTableField(bye, "标签")
		if existt {
			article.Tags = values
			continue
		}

		exist, value = readNormalField(bye, "创建时间")
		if exist {
			article.Time = value
			continue
		}

		exist, value = readNormalField(bye, "简介")
		if exist {
			article.Introduction = value
			continue
		}

		exist, value = readCoverField(bye)
		if exist {
			article.Cover = value
			break
		}
	}

	article.ContentByte = mdByte

over:
	return article
}

// 是否跳过读取
func isSkip(bt []byte) bool {
	if len(bt) == 0 || bytes.Contains(bt, []byte("---")) {
		return true
	}
	return false
}

// 文章是否发布
func isRelease(bt []byte) bool {
	if bytes.Contains(bt, []byte("发布状态 :")) && bytes.Contains(bt, []byte("是")) {
		return true
	}
	return false
}

// 读取隐藏字段
func readHideField(bt []byte, sch string) (bool, string) {
	if !bytes.Contains(bt, []byte(sch)) {
		return false, ""
	}
	spl := bytes.Split(bt, []byte(":"))
	if len(spl) != 2 {
		return false, ""
	}
	value := spl[1]
	bt = bytes.ReplaceAll(value, []byte(" "), []byte(""))
	bt = bytes.TrimRight(bt, "-->")
	return true, string(bt)
}

// 读取显示字段
func readNormalField(bt []byte, sch string) (bool, string) {
	if !bytes.Contains(bt, []byte(sch)) {
		return false, ""
	}
	spl := bytes.Split(bt, []byte(":"))
	if len(spl) != 2 {
		return false, ""
	}
	value := spl[1]
	bt = bytes.ReplaceAll(value, []byte(" "), []byte(""))
	bt = bytes.ReplaceAll(bt, []byte("`"), []byte(""))
	return true, string(bt)
}

// 读取数组字段
func readTableField(bt []byte, sch string) (bool, []string) {
	if !bytes.Contains(bt, []byte(sch)) {
		return false, nil
	}
	spl := bytes.Split(bt, []byte(":"))
	if len(spl) != 2 {
		return false, nil
	}
	value := spl[1]
	value = bytes.ReplaceAll(value, []byte(" "), []byte(""))
	value = bytes.ReplaceAll(value, []byte("`"), []byte(""))
	spl = bytes.Split(value, []byte("|"))

	var strs = []string{}
	for _, v := range spl {
		strs = append(strs, string(v))
	}
	return true, strs
}

// 读取插图(![][] or ![]())
func readCoverField(bt []byte) (bool, string) {
	state, type1 := IsPicURL(bt)
	if !state {
		return false, ""
	}

	// 远程图片Url
	if type1 == 1 {
		buf := string(bt)
		st := strings.Index(buf, "(")
		ed := strings.Index(buf, ")")
		return true, buf[st+1 : ed]
	}

	// 本地图片URL
	buf := string(bt)
	st := strings.Index(buf, ":")
	ed := len(buf)
	return true, buf[st+1 : ed]
}

func readCode(bt *[]byte) bool {
	if !bytes.Contains(*bt, []byte("```")) {
		return false
	}

	buf := string(*bt)
	*bt = []byte(buf[1:3])
	return true
}

// IsPicURL 是否是图片url，格式为![][] or ![]()
// @return bool true是图片，false不是
// @return int 1为远程url，2为本地url
func IsPicURL(bye []byte) (bool, int) {

	// ![]() => ![name](http://xxx.xxx.png)
	if bytes.Contains(bye, []byte("![")) && bytes.Contains(bye, []byte("](")) {
		return true, 1
	}

	// ![][] => [name]:res/pic.png
	if bytes.Contains(bye, []byte("[")) && bytes.Contains(bye, []byte("]:")) {
		return true, 2
	}
	return false, 0
}
